<?php

namespace App\Http\Controllers\Admin;

use App;
use App\Http\Controllers\Controller as BaseController;
use App\Http\Logic\Admin\BusinessLogic;
use App\Http\Requests\BusinessRequest;
use App\Common\Status;
use Illuminate\Http\Request;

class BusinessController extends BaseController
{

    /**
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request): \Illuminate\Http\JsonResponse
    {
        $params   = $request->query();
        $pageSize = $request->query("pageSize", Status::PAGE_SIZE);
        $page     = $request->query("page", Status::PAGE);
        $data     = BusinessLogic::getBusinessList($params, $pageSize, $page);
        return $this->success(Status::SUCCESS, 'success', $data);
    }


    /**
     * @param BusinessRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function createBusiness(BusinessRequest $request): \Illuminate\Http\JsonResponse
    {
        $params = $request->validated();
        $id     = BusinessLogic::createBusiness($params);
        $r      = $params['id'] ? [] : ['id' => $id];
        return $this->success(Status::SUCCESS, 'success', $r);
    }


    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function dtlBusiness($id): \Illuminate\Http\JsonResponse
    {
        $info = BusinessLogic::getBusinessDtl($id);
        return $this->success(Status::SUCCESS, 'success', $info);
    }


    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delBusiness($id): \Illuminate\Http\JsonResponse
    {
        BusinessLogic::delBusiness($id);
        return $this->success(Status::SUCCESS, 'success', []);
    }


    /**
     * @param $id
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function stBusiness($id, Request $request): \Illuminate\Http\JsonResponse
    {
        $type = $request->query("type");
        BusinessLogic::setStatus($id, $type);
        return $this->success(Status::SUCCESS, 'success', []);
    }
}
